//
//  AlarmView.swift
//  TLCLockDemo
//
//  Created by run on 2024/7/24.
//

import SwiftUI
import TLCLock

struct AlarmView: View {


    let device: LockDevice

    var body: some View {
        
        VStack {
            YxrActionView(
                data: [
                    (0, "开启"),
                    (1, "关闭"),
                ], column: 3,
                itemBuilder: { value in
                    YXRActionItem(text: value.1) {
                        switch value.0 {
                        case 0: alarm(isOpen: true)
                        case 1: alarm(isOpen: false)
                        default:
                            print("未实现的方法")
                        }
                    }
                }
            )
            .padding(.horizontal, 10)
            
            LogView()
        }
        
        .yxrNavigationTitle("防撬警报")
    }

    func alarm(isOpen: Bool) {
        guard let lockKey = Server.getSecret(device.macAddress) else {
            showToast("没有lockKey")
            return
        }
        lockTask(desc: "防撬警报") {
            await device.alarm(isOpen: isOpen, lockKey: lockKey, trackId: Server.testTrackId)
        }
    }
}
